//
//  AppDelegate.m
//  Sampux
//
//  Created by rzm on 16/7/1.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "AppDelegate.h"
#import "SampuxLoginViewController.h"
#import "SampuxCustomNavController.h"

#define kStoreAppId @"1154356157"
@interface AppDelegate ()<UIAlertViewDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 

    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    UIWindow *window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window = window;
    self.window.backgroundColor = [UIColor whiteColor];
    SampuxLoginViewController *loginViewCtr = [[SampuxLoginViewController alloc]init];
    SampuxCustomNavController *loginVCNav = [[SampuxCustomNavController alloc]initWithRootViewController:loginViewCtr] ;
    self.window.rootViewController =  loginVCNav;

    self.launcher = [[GBInterMaster_Launcher alloc] initWithHost:HOST_ADDRESS withPort:HOST_PORT];
    [self.launcher registerNotificationReceiver];
    [self.launcher connectToServer];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"getWiFiNameWithSetWiFi" object:nil];
    
    [self checkAppUpdate];
    [self.window makeKeyAndVisible];
    return YES;
}

#pragma mark 检查版本更新
- (void)checkAppUpdate
{
    NSString *nowVersion = [SampuxGlobalTool getApplicationVersionCode];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",kStoreAppId]];
    
    NSString * file = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

    NSRange substr = [file rangeOfString:@"\"version\":\""];
    NSRange range1 = NSMakeRange(substr.location+substr.length,10);
    NSRange substr2 = [file rangeOfString:@"\"" options:NSCaseInsensitiveSearch range:range1];
    NSRange range2 = NSMakeRange(substr.location+substr.length, substr2.location-substr.location-substr.length);
    NSString *newVersion = [file substringWithRange:range2];
    if(![nowVersion isEqualToString:newVersion])
    {
        /**
         *  版本更新
         */
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"有新版本发布了,去看看吧" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        alert.delegate = self;
    } else {
//        [SVProgressHUD showSuccessWithStatus:@"当前已经是最新版本"];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/us/app/sang-pu-cai-nuan/id1154356157?mt=8"]];
    }

}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
//    [self.launcher connectToServer];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"getWiFiNameWithSetWiFi" object:nil];

}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
